package top.chenfu1201.clientApi.controller.mapper;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import top.chenfu1201.clientApi.vo.UserVo;

@RequestMapping("/mapper/user")
@RestController(value = "mapperUserController")
public class UserController {

    /**
     * 增加日志输出
     */
    private static final Logger log = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private RestTemplate restTemplate;

    /**
     * 请求mapper项目的URL
     */
    @Value("${mapper.base.user.url}")
    public String baseMapperUrl;

    @GetMapping("/queryAll")
    public Object queryAll() {
        log.info("queryAll");
        return this.restTemplate.getForObject(baseMapperUrl + "queryAll", JSONArray.class);
    }

    @GetMapping("/find/{userId}")
    public Object findUserById(@PathVariable Long userId) {
//        JSONObject result = this.restTemplate.getForObject(baseMapperUrl + "find/{userId}", JSONObject.class, userId);
        log.info("findUserById入参userId=" + userId);
        ResponseEntity<JSONObject> result = this.restTemplate.getForEntity(baseMapperUrl + "find/{userId}", JSONObject.class, userId);
        return result;
    }

    @PostMapping("/add")
    public Object addUser(@RequestBody UserVo userVo) {
        log.info("addUser入参userVo=" + JSON.toJSONString(userVo));
//        JSONObject result = this.restTemplate.postForObject(baseMapperUrl + "add", userVo, JSONObject.class);
        ResponseEntity<JSONObject> result = this.restTemplate.postForEntity(baseMapperUrl + "add", userVo, JSONObject.class);
//        return jsonObject.toJSONString();
        return result;
    }

    @GetMapping("/find")
    public Object findUser(UserVo userVo) {
        Integer result = 5 / 0;
        return result;
    }

}
